package g4meTwo;

import java.awt.Dimension;
import java.io.IOException;
import java.util.Random;

import Jeux.Joueur;
import Jeux.Joystick;
import Jeux.MiniJeux;
import Jeux.SimpleFont;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;


public class Cave extends MiniJeux {
	private int temps = 0;
	private MobileItem rock;
	private MobileItem redSquare = null;
	private MobileItem redRectangle = null;
	private MobileItem redRectangle2 = null;
	Random random = new Random();
	private int directionRedSquare = random.nextInt(8);
	private int directionRedRectangle = random.nextInt(8);
	private int directionRedRectangle2 = random.nextInt(8);
	private float freeze = 2000;
	float scale = 1.0f;
	private boolean gameOver=false;
	private double score=0;
	Joystick joyStick;
	private boolean hardMode = true;
	private boolean assist = true;
	private float k = 1;
	private double a = 1;
	private double h = 1;
	private SimpleFont font;
	private boolean contraste;

	public Cave(String title,Joueur joueur, boolean hardMode, boolean assist, boolean contrast) {
		super(title);
		this.contraste = contrast;
		this.joueur=joueur;
		this.hardMode = hardMode;
		this.assist = assist;
		Dimension tailleEcran = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int hauteur = (int)tailleEcran.getHeight();
		int largeur = (int)tailleEcran.getWidth();
		try{
			 app = new AppGameContainer(this,largeur,hauteur,false); 
			 app.setForceExit(false);
	         app.start();
		}catch(SlickException e){
			System.out.println(e);
		}
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		font=new SimpleFont("Arial",1, 40);
		joyStick=new Joystick();
		gc.setPaused(false);
		if(hardMode){
			h = 2;
		}
		if(assist){
			a = 0.5;
		}
		temps = 0;
		freeze = 2000;
		k = 1;
		gameOver = false;
		score = 0;
		rock = new MobileItem("rock",false, 800, 450, 45, 45,
				"ressources/images/rockContrast.jpg");
		redSquare = new MobileItem("redSquare",false, 0, 0, 45, 45,
				"ressources/images/RedSquare.jpg");
		redRectangle = new MobileItem("redRectangle",false, 600, 800, 75, 32, "ressources/images/RedRectangle.jpg");
		redRectangle2 = new MobileItem("redRectangle2",false, 400, 500, 75, 32, "ressources/images/RedRectangle.jpg");


	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		super.update(gc,delta);
		Input input = gc.getInput();
		float m = 1;
		if((input.isKeyDown(Input.KEY_SPACE) || (assist && (rock.near(redSquare,50) || rock.near(redRectangle,50) || rock.near(redRectangle2,50))))&& freeze > 0)
		{
			delta = delta/3;
			freeze -= delta*3;
			m = (float) 1.5;
		}
		float hip = 0.2f * delta;
		temps += delta;

		if (!rock.near(redSquare,0) && !rock.near(redRectangle,0) && !rock.near(redRectangle2,0)) {
			if ((temps+1) % 70 == 0) {
				directionRedSquare = random.nextInt(8);
				directionRedRectangle = random.nextInt(8);
				directionRedRectangle2 = random.nextInt(8);
				if(hardMode){ 
					if(k < 2){
						k += 0.05;
					}
					if(k > 2){
						k += 0.025;
					}
					freeze += 50;
				}
			}
			redSquare.autoMove(directionRedSquare, k*hip, gc);
			redRectangle.autoMove(directionRedRectangle, k*hip, gc);
			redRectangle2.autoMove(directionRedRectangle2, k*hip, gc);

			if (input.isKeyDown(Input.KEY_Z) || input.isKeyDown(Input.KEY_UP)) {
				rock.borderY(gc);
				rock.setY(rock.getY() - 4 *m* hip);
				rock.update();
			}
			if (input.isKeyDown(Input.KEY_S) || input.isKeyDown(Input.KEY_DOWN)) {
				rock.borderY(gc);
				rock.setY(rock.getY() + 4 *m* hip);
				rock.update();
			}
			if (input.isKeyDown(Input.KEY_Q)|| input.isKeyDown(Input.KEY_LEFT)) {
				rock.borderX(gc);
				rock.setX(rock.getX() - 4 *m* hip);
				rock.update();
			}
			if (input.isKeyDown(Input.KEY_D) || input.isKeyDown(Input.KEY_RIGHT)) {
				rock.borderX(gc);
				rock.setX(rock.getX() + 4 *m* hip);
				rock.update();
			}


		}

		else {
			gameOver=true;
			if(score==0){
				score=temps*a*h;
				gc.setPaused(true);
			}
		}
	}


	public void render(GameContainer gc, Graphics g) throws SlickException {
		
		String sec=((temps/1000)%60<10)?"0"+Integer.toString((temps/1000)%60):Integer.toString((temps/1000)%60);
		String min=((temps/1000)/60<10)?"0"+Integer.toString((temps/1000)/60):Integer.toString((temps/1000)/60);
		if(contraste == false)
		{
			rock.draw(rock.getX1(), rock.getY1(), scale,Color.black);
			g.drawImage(new Image("ressources/images/ralentir.gif"), 0, 8*gc.getHeight()/10);
			font.get().drawString(80,8*gc.getHeight()/10,freeze/1000+"s",Color.black);
			font.get().drawString(gc.getWidth()/2,  gc.getHeight()/11, min+":"+sec,Color.black);
			g.setBackground(Color.white);
			g.setColor(Color.black);
		}
		if(contraste == true)
		{
			rock.draw(rock.getX1(), rock.getY1(), scale);
			g.drawImage(new Image("ressources/images/ralentir.gif"), 0, 8*gc.getHeight()/10,Color.white);
			font.get().drawString(gc.getWidth()/2,  gc.getHeight()/11, min+":"+sec,Color.white);
			font.get().drawString(80,8*gc.getHeight()/10,freeze/1000+"s", Color.white);
			g.setBackground(Color.black);
			g.setColor(Color.white);
		}
		redSquare.draw(redSquare.getX1(), redSquare.getY1(), scale);
		redRectangle.draw(redRectangle.getX1(),redRectangle.getY1(), scale);
		redRectangle2.draw(redRectangle2.getX1(),redRectangle2.getY1(), scale);
		//g.drawString("x : " + redSquare.getX() + "y : " + redSquare.getY(), 130, 130);
		if(freeze<0){
			freeze = 0;
		}
		if (gameOver==true){
			String scored = Double.toString(score);
			if(this.contraste == true)
			{
				font.get().drawString(gc.getWidth()/2-scored.length()*10, gc.getHeight()/2, "SCORE:"+score+" !");
			}
			if(this.contraste == false)
			{
				font.get().drawString(gc.getWidth()/2-scored.length()*10, gc.getHeight()/2, "SCORE:"+score+" !",Color.black);

			}
			
		}

	}
}
